/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "server.h"
#include "stub.h"

#include "../net/msgserversocketconnection.h"
#include "../io/ioexception.h"
#include "../io/processexception.h"
#include "echolistener.h"

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace greenhills::net;

#if defined Q_OS_LINUX
#include <termios.h>
#include <unistd.h>

#define NB_DISABLE 0
#define NB_ENABLE 1

void nonblock(int state)
{
    struct termios ttystate;

    //get the terminal state
    tcgetattr(STDIN_FILENO, &ttystate);

    if (state==NB_ENABLE)
    {
        //turn off canonical mode
        ttystate.c_lflag &= ~ICANON;
        //minimum of number input read.
        ttystate.c_cc[VMIN] = 1;
    }
    else if (state==NB_DISABLE)
    {
        //turn on canonical mode
        ttystate.c_lflag |= ICANON;
    }
    //set the terminal attributes.
    tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);

}

int kbhit()
{
    struct timeval tv;
    fd_set fds;
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
    select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    return FD_ISSET(STDIN_FILENO, &fds);
}
#elif defined Q_OS_WIN32
    #include <conio.h>
    #define kbhit(x) _kbhit(x)
#elif defined Q_OS_WINCE
    int kbhit() {return 0;}
#endif

//== Tests =====================================================================
//== End of Tests ==============================================================

int main(/*int argc, char *argv[]*/)
{
    using namespace greenhills::server;

#if defined Q_OS_LINUX
    nonblock(NB_ENABLE);
#endif

    std::cout << "Server is running..." << std::endl;

    CServer server;
    CStub stub(server);

    bool terminated = false;
    try {
        stub.open();
        while (!terminated) {
            stub.process();
#if defined Q_OS_LINUX
            usleep(10);
#elif defined Q_OS_WIN32
            Sleep(10);
#elif defined Q_OS_WINCE
            Sleep(10);
#endif
            if (kbhit() != 0)
            {
                char c=fgetc(stdin);
                switch(c)
                {
                case 'q':
                    terminated = true;
                    std::cout << " - Terminating..." << std::endl;
                    break;
                case 'h':
                    std::cout << " - Help" << std::endl;
                    std::cout << "    q <quit>" << std::endl;
                    std::cout << "    h <help>" << std::endl;
                    break;
                default:
                    std::cout << " - Unknown Command. Type h for help."
                            << std::endl;
                    break;
                }
            }
        }
    } catch (greenhills::io::CIOException ioe) {
        cerr << ioe.toString() << endl;
    }
    stub.close();

    std::cout << "Server terminated" << std::endl;

#if defined Q_OS_LINUX
    nonblock(NB_DISABLE);
#endif

    return 0;
}
